package it.poste.il.compensatore.web.servlet;

import it.poste.il.compensatore.web.httpclient.CompensatoreHttpClient;

import java.io.BufferedReader;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MyServlet
 */
@WebServlet("/CompensatoreServlet")
public class CompensatoreServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CompensatoreServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		
		StringBuffer jb = new StringBuffer();
		  String line = null;
		  try {
		    BufferedReader reader = request.getReader();
		    while ((line = reader.readLine()) != null)
		      jb.append(line);
		  } catch (Exception e) { e.printStackTrace(); }

	;
	
		 System.out.println(jb);
		  
		  try {  
	            
//	            MessageFactory msgFactory     = MessageFactory.newInstance();  
//	            SOAPMessage message           = msgFactory.createMessage();  
//	            SOAPPart soapPart             = message.getSOAPPart();  
//	   
//	            
//	            byte[] buffer                 = jb.toString().getBytes();  
//	            ByteArrayInputStream stream   = new ByteArrayInputStream(buffer);  
//	            StreamSource source           = new StreamSource(stream);  
//	   
//	              
//	            soapPart.setContent(source);  
//	   
//	           
//	   
//	            message.writeTo(System.out); 
			  String r=  
	          CompensatoreHttpClient.sendRequest("http://localhost:9090/saldolistamovimento",request.getHeader("SOAPAction"),jb.toString());
			  
			  ServletOutputStream respStream=response.getOutputStream();
			  respStream.write(r.getBytes());
			  respStream.close();
			  
			  
			
	   
	        } catch (IOException  e) {  
	            System.out.println("IOException : " + e);  
	        } 
		  
		  
		  
	}

}
